package com.hxgz.steel.service.Impl;

import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.BackendUserMapper;
import com.hxgz.steel.domain.BackendUser;
import com.hxgz.steel.service.BackendLoginerService;
import com.hxgz.steel.util.MD5Util;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BackendLoginerServiceImpl implements BackendLoginerService {

  @Autowired
  BackendUserMapper backendUserMapper;

  @Override
  public ServerResponse getbackendLoginerList() {
    List<BackendUser> list = backendUserMapper.getbackendLoginerList();
    return ServerResponse.createBySuccess(list);
  }

  @Override
  @Transactional
  public ServerResponse addLoginer(String realName, String userName, String password, String id) {
    int i = -3;
    if (id != null) {
      BackendUser backendUser = backendUserMapper.selectByPrimaryKey(Long.valueOf(id));
      backendUser.setPassword(MD5Util.MD5EncodeUtf8(password));
      i = backendUserMapper.updateByPrimaryKeySelective(backendUser);
    } else {
      BackendUser backendUser = new BackendUser();
      backendUser.setRealName(realName);
      backendUser.setPassword(MD5Util.MD5EncodeUtf8(password));
      backendUser.setUserName(Long.valueOf(userName));
      backendUser.setRole(1);
      i = backendUserMapper.insertSelective(backendUser);
    }
    return i == 1 ? ServerResponse.createBySuccessMessage("操作成功") : ServerResponse.createByError();
  }

  @Override
  @Transactional
  public ServerResponse deleteLoginer(String id) {
    int i = backendUserMapper.deleteByPrimaryKey(Long.valueOf(id));
    return i == 1 ? ServerResponse.createBySuccessMessage("删除成功") : ServerResponse.createByError();
  }


}
